home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
software
/
nn_prepr
/
nlds.wgz
< prev
Wrap
Text File
|
1992-09-21
|
15KB
|
488 lines
Scripts for Nonlinear Analysis of Time-series
FUNCTION Hurst(closecolumn,rowstart,rowend,differences,N,
start_N,delta_N,rscolumn,report,graph)
{ script to calculate the Hurst coefficient and}
{where}
{ N = the number of windows (number of graph points on (R/S,N)}
{ N_start = initial value of N or size of first window}
{ delta_N = arithmetic increment in window size}
{ or if delta_N=0 then geometric increment in window size}
{ Eu(t) = the influx for each t}
{ M(N) = the average influx for each N}
{ X(t,N) = the difference between Eu(t) and M(N)}
{ R(N) = the difference between the Max(Xt,N) and Min(Xt,N)}
{ R/S or H = the rescaled range or R(N) / stdev.}
{ b0 = the intercept of log(R/S) vs log(N)}
{ b1 = the slope of log(R/S) vs log(N)}
{ f = fractal dimension of the time-series or 1/H}
{ differences = flag for calculating Eu over series or first}
{ difference of series}
{ report = flag for recording details of R/S computations}
{ graph = flag for plotting a scatter diagram of log(r/s) vs log(N)}
{Hurst coefficient}
{-> if H >0.5 persistent trends,}
{-> if H = 0.5 random}
{-> if H <0.5 anti-persistent trends}
Column Numbers
Manual Recalc
define i,j,k,l,counter,no_of_records,step_size,no_of_windows, rowcounter
define M,S,Eu,Xt,R,RS,H, lnRS,lnN, f,b0,b1, Max_of_Xt, Min_of_Xt, totalXt
define change_Eu,windowsize,deltaN, end_row
no_of_records = rowend-rowstart+1
step_size=start_N
{column labels}
if report = 1
put "Eu" into makecell(rscolumn,1)
put "M" into makecell(rscolumn+1,1)
put "S" into makecell(rscolumn+2,1)
put "Xt" into makecell(rscolumn+3,1)
put "sumX" into makecell(rscolumn+4,1)
put "R" into makecell(rscolumn+5,1)
put "R/S" into makecell(rscolumn+6,1)
end if
{ calculate the differences from t to t+1}
for i = rowstart+1 to rowend
if differences = 1
Eu = ln(indirect(makecell(closecolumn,i))
/indirect(makecell(closecolumn,i-1)))
put Eu into makecell(rscolumn,i)
else
put indirect(makecell(closecolumn,i)) into makecell(rscolumn,i)
end if
end for
FOR counter = 1 to N
{calculate for each N:
the number of windows,the windowsize starting with start_N}
if delta_N = 0
{if delta_N is put to zero then value of N will double}
windowsize = start_N^counter
else
{if start_n = 2 and delta_N = 2 then when N = 2,
windowsize = 2+ 2*(2-1) =4}
windowsize = start_N + delta_N * (counter-1)
end if
no_of_windows = int((no_of_records-1)/ windowsize)
put counter into makecell(rscolumn-1,1)
{calculate the last low for reach windowsize}
end_row = rowstart+(no_of_windows*windowsize)
FOR i = rowstart+1 TO end_row step windowsize
{calculate M and S for the current windowsize}
put i into makecell(rscolumn-1,2)
M = avg(range(makerange(rscolumn,i, rscolumn,i+windowsize-1)))
S = stdev(range(makerange(rscolumn,i, rscolumn,i+windowsize-1)))
if report = 1
put M into makecell(rscolumn+1,i)
put S into makecell(rscolumn+2,i)
end if
max_of_Xt = -10000000
min_of_Xt = 10000000
totalXt= 0
{calculate for each N AND for each window Xt, R and RS}
FOR j = i TO i+windowsize-1
Eu = indirect(makecell(rscolumn,j))
{Calculate X(t,N) or e(u)-M for N windows}
Xt = Eu - M
put Xt into makecell(rscolumn+3,j)
put j into makecell(rscolumn-1,3)
totalXt=totalXt + Xt
put totalXt into makecell(rscolumn+4,j)
END FOR { end of computations for a single window}
{calculate R (N) for N windows}
max_of_Xt=max((range(
makerange(rscolumn+4,i, rscolumn+4,i+windowsize-1))))
min_of_Xt=min((range(
makerange(rscolumn+4,i, rscolumn+4,i+windowsize-1))))
put max_of_Xt into makecell(rscolumn+7,i)
put min_of_Xt into makecell(rscolumn+8,i)
{calculate R, RS}
R = (max_of_Xt) - (min_of_Xt)
if S = 0
RS=R/0.000000000000001 {avoids division by 0}
else
RS = R/S
end if
put R into makecell(rscolumn+5,i)
put RS into makecell(rscolumn+6,i)
END FOR {end of computation for a single N}
{report summary results N, ln N, R/S, ln R/S}
put "data point" into makecell(rscolumn+10,1)
put " N " into makecell(rscolumn+11,1)
put " R/S " into makecell (rscolumn+12,1)
put " log R/S " into makecell (rscolumn+13,1)
put "log N" into makecell(rscolumn+14,1)
put "Column = " into makecell(rscolumn+15,1)
put closecolumn into makecell(rscolumn+16,1)
select range(makerange(rscolumn+10,1,rscolumn+16,1))
align center
text style "B"
unselect
put counter into makecell(rscolumn+10,counter+1)
{ record N}
put windowsize into makecell(rscolumn+11,counter+1)
{ record average or R/S values}
RS = avg(range(makerange(rscolumn+6,rowstart, rscolumn+6,rowend)))
put RS into makecell(rscolumn+12,counter+1)
put log(RS) into makecell(rscolumn+13,counter+1)
put log(windowsize) into makecell(rscolumn+14,counter+1)
{ compute the H coefficient for each N}
{clean up the worksheet :erase sumX & RS values}
select range(makerange(rscolumn+1,rowstart, rscolumn+8,rowend))
clear
unselect
select range(makecell(rscolumn-1,1))
unselect
END FOR {end of computations for all N's}
{ run regression of log(R/S) vs log (N)}
counter = 0
for i = 2 to N+1
if indirect(makecell(rscolumn+13,i)) <= 0.5
counter = counter +1
end if
end for
select range(makerange(rscolumn+13,2, rscolumn+14,counter))
select more range(makerange(rscolumn+13,N+3, rscolumn+14,N+3))
select more range(makerange(rscolumn+13,N+5, rscolumn+20,N+10))
regress
put "log(a)" into makecell(rscolumn+13,N+2)
put "Hurst" into makecell(rscolumn+14,N+2)
put "Correlation" into makecell(rscolumn+15,N+2)
H = indirect(makecell(rscolumn+14,N+3))
put 2^(2*H-1)-1 into makecell(rscolumn+15,N+3)
select range(makerange(rscolumn+13,N+3, rscolumn+15,N+3))
precision 4
unselect
{plot a scatter diagram of log(R/S) vs log(N)}
if graph = 1
{copy the log R/S and log (N)}
select range(makerange(rscolumn+13,2,rscolumn+13,N+2))
copy
go to cell makecell(rscolumn+20,2)
paste
select range(makerange(rscolumn+14,2,rscolumn+14,N+2))
copy
go to cell makecell(rscolumn+19,2)
paste
{plot a scatter; adjust the scales on the axis}
Unselect
Add Chart Range frac(R1C25..R28C30,43,1,64,252)
Using range(makerange(rscolumn+19,2,rscolumn+20,N+2))
object name "R/Splot"
select object "R/Splot"
Scatter
select chart "R/Splot" axis 3
manual scaling from 0 to 1 with 10 major and 5 minor divisions
axis log base 10
select chart "R/Splot" axis 1
manual scaling from 0 to 5 with 5 major and 5 minor divisions
axis log base 10
Hide Legend
end if
{ save file}
save
END FUNCTION
FUNCTION CorrDim(col,rowstart,rowend,start_dimem,end_dimem,
tau,dt,R,rscolumn)
{ script to calculate the Correlation Dimension as}
{ defined by Grassberger & Procaccia}
{ where}
{ npt = number of observations}
{ dimem = embedding dimensions}
{ tau = lag time for phase space}
{ dt = increase in each measurement}
{ R = the beginning distance}
{ CR = correlation integral}
{ lag = minimum time between pairs}
{ col = input data column}
Column Numbers
Manual Recalc
Define i,j,k, npt, ind, total,its, z,d
Define theta,theta2, CR, lag,rownumber, colnumber, L
Define stop1,dimem
Define loop
{this loop provides for multiple estimates of corr dim}
for loop = start_dimem to end_dimem
dimem = loop
if loop > start_dimem
rscolumn = rscolumn + 10
end if
theta = 0
theta2 = 0
CR = 0
ind = 1
k = 1
lag = 0
total = 0
its = 0
npt = rowend - rowstart + 1
rownumber =1
colnumber = 10
{read the input column and reconstruct the phase space}
for i = rowstart to rowstart+npt
for j = 1 to dimem
{read x(i+(j-1) * tau and put it into a}
{ working area z(i,j) on the sheet}
z = indirect(makecell(col,i +(j-1)*tau))
put z into makecell(rscolumn+colnumber+j,i)
end for
end for
{ calculate the maximum length of the phase space}
npt = npt -dimem*tau
stop1 = 0
while (stop1 = 0)
for k = rowstart to rowend
for i = rowstart to rowend
d = 0
for j = 1 to dimem
{calculating the square of distance}
d = d + (indirect(makecell(rscolumn+colnumber+j,i-lag))
- indirect(makecell(rscolumn+ colnumber+j,i))) ^2
end for
d = sqrt(d)
if d > R
theta2 = 0
else
theta2 = 1
end if
theta = theta + theta2
end for
lag = lag +1
end for
{calc correlation integral}
CR = (1/npt^2)*theta
{produce result table}
rownumber = rownumber + 1
put "Counter" into makecell(rscolumn,1)
put "Correlation Integral" into makecell(rscolumn+1 ,1)
put " Distance R" into makecell(rscolumn+2,1)
put "log(CR)" into makecell(rscolumn+3, 1)
put "log(R)" into makecell(rscolumn+4,1)
put rownumber-1 into makecell(rscolumn, rownumber)
put CR into makecell (rscolumn+1,rownumber)
put R into makecell (rscolumn+2, rownumber)
put log(cr) into makecell(rscolumn+3, rownumber)
put log(R) into makecell(rscolumn+4,rownumber)
L = L + 1
if L > 20
stop1 =1
else
R = R + dt
CR = 0
theta = 0
theta2 = 0
lag = 0
end if
end while
{format results}
select range(makerange(rscolumn, 1, rscolumn,rownumber))
precision 0
select range(makerange(rscolumn+1, 1, rscolumn+4,rownumber))
precision 6
unselect
{ clean up spreadsheet}
select range(makerange(
rscolumn+colnumber, rowstart, rscolumn+colnumber+dimem,rowend))
clear
unselect
{ calc the regression}
select range(makerange(rscolumn+3, 2, rscolumn+4,rownumber))
select more range(makerange(
rscolumn+3, rownumber+2, rscolumn+4,rownumber+2))
select more range(makerange(
rscolumn+3, rownumber+4, rscolumn+10,rownumber+10))
regress
select range(makerange(
rscolumn+3, rownumber+2, rscolumn+5,rownumber+12))
precision 6
unselect
end for
END FUNCTION
FUNCTION Lyapunov(col,rowstart,rowend,start_dimem,end_dimem,tau,dt,
scalmax,scalmin,evolve,lag,rscolumn)
{ script to calculate the Lynapunov exponent }
{ where}
{ npt = number of observations}
{ dimem = embedding dimensions}
{ tau = lag time for phase space}
{ dt = increase in each measurement}
{ scalmax = maximum divergence}
{ scalmin = minimum divergence}
{ evolve = evolution time}
*{ lag = minimum time between pairs}
{ col = input data column}
{ wrkcol = first working column}
{ wrkrow = first working row cell}
Column Numbers
Manual Recalc
Define i,j, npt, ind,ind2,indold, total,its, pt1,pt2, z
Define zlyap, zmult, anglmx,thmin,iii,dnew,dot,cth,d,di,dii,df,th
Define dummy,rownumber, dimem
Define stop1,stop2,loop
for loop = start_dimem to end_dimem
dimem = loop
if loop > start_dimem
rscolumn = rscolumn +1
end if
npt = rowend - rowstart +1
ind = 1
total = 0
its = 0
rownumber = 1
{read the input column and reconstruct the phase space}
for i = rowstart to rowstart + npt-(dimem-1)*tau
for j = 1 to dimem
{read x(i+(j-1) * tau and put it into a
working area on the sheet}
z = indirect(makecell(col,i +(j-1)*tau))
put z into makecell(rscolumn+10+j,i)
end for
end for
{max length of phase space}
npt = npt -dimem*tau-evolve
di = 1000000000
{find initial pair}
for i = rowstart+lag +1 to rowstart+npt
d = 0
for j = 1 to dimem
{calc distance}
d = d + (indirect(makecell(rscolumn+10+ind,i))
- indirect(makecell(rscolumn+10+j,i)) )^2
end for
d =sqrt(D)
if d > di or d < scalmin
continue for { store the best point}
end if
di = d
ind2 = i
end for
{coordinates of evolve}
stop1 = 0
stop2 = 0
while (stop1 = 0)
for j = 1 to dimem
pt1 = indirect(makecell(j,ind+evolve))
put pt1 into makecell(rscolumn+20,j)
pt2 = indirect(makecell(j, ind2+evolve))
put pt2 into makecell(rscolumn+21,j)
end for
df = 0
{compute final divergence}
for j = 1 to dimem
df = df+ (indirect(makecell(rscolumn+21,j))
- indirect(makecell(rscolumn+20,j)) )^2
end for
df = sqrt(df)
its = its +1
total = total + (log(df/di)/(evolve*dt*log(2)))
zlyap =total/its
{produce output table}
rownumber = rownumber +1
put "zlyap" into makecell(rscolumn,1 )
put "evolve*its" into makecell(rscolumn+1,1 )
put "di" into makecell(rscolumn+2,1 )
put "df" into makecell(rscolumn+3,1 )
put zlyap into makecell(rscolumn,rownumber)
put evolve*its into makecell(rscolumn+1,rownumber )
put di into makecell(rscolumn+2,rownumber )
put df into makecell(rscolumn+3,rownumber )
indold = ind2
zmult = 1
anglmx =.3
stop2=0
while (stop1 = 0) and (stop2 = 0)
thmin = 3.14
{look for replacement points}
for i =rowstart to rowstart+npt
iii = abs(int(i-(ind+evolve)))
{reject if replacement point is too close to original}
if iii < lag
continue for
end if
dnew = 0
for j = 1 to dimem
dnew = dnew+ (indirect(makecell(rscolumn+20,j))
- indirect(makecell(rscolumn+10+j,i)) ) ^2
end for
dnew = sqrt(dnew)
if (dnew > zmult * scalmax) or ( dnew < scalmin)
continue for
end if
dot = 0
for j = 1 to dimem
{calc pt1 - pt2}
dummy = indirect(makecell(rscolumn+20,j))
-indirect(makecell(rscolumn+21,j))
dot = dot + (indirect(makecell(rscolumn+20,j))
-indirect(makecell(rscolumn+10+j,i)) * dummy)
end for
cth = abs(dot/dnew*df)
if cth > 1
cth =1
end if
th = cos(cth)
if th > thmin
continue for
end if
thmin = th
dii = dnew
ind2 = i
end for
if (thmin < anglmx)
ind =ind + evolve
if (ind >= npt)
stop1 = 1
stop2 = 1
else
di=dii
stop2 = 1
continue while
end if
else {case when thmin => anglmx}
zmult = zmult + 1
if (zmult < 5)
stop2 = 0
continue while
else { case when zmult => 5}
zmult = 1
anglmx = 2*anglmx
if (anglmx < 3.14)
stop2 = 0
continue while
else {case when anglmx => 3.14}
ind2=indold+evolve
dii=df
ind=ind+evolve
if ( ind >= npt)
stop1 = 1
stop2 = 1
else
di = dii
stop2 = 1
continue while
end if
end if
end if
end if
end while
end while
{clean up the worksheet}
select range(makerange(rscolumn+10,rowstart,rscolumn+21,rowend))
clear
unselect
{run regression on lynapunov exponent}
select range(makerange(
rscolumn,rownumber-20,rscolumn+1,rownumber))
select more range(makerange(
rscolumn,rownumber+2,rscolumn+1,rownumber+2))
select more range(makerange(
rscolumn,rownumber+4,rscolumn+5,rownumber+5))
regress
end for {for loop}
Return("done")
END FUNCTION